package com.rockcent.service.impl;

import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.domain.Customer;
import com.rockcent.mall.lib.domain.Evaluation;
import com.rockcent.mall.lib.domain.MerchantOrder;
import com.rockcent.mall.lib.domain.Product;
import com.rockcent.mall.lib.domain.enumclass.EvaluateStatus;
import com.rockcent.mall.lib.domain.enumclass.MerchantOrderMerchantRole;
import com.rockcent.mall.lib.domain.enumclass.ProductClientType;
import com.rockcent.mall.lib.dto.EvaluationDto;
import com.rockcent.mall.lib.dto.ProductDto;
import com.rockcent.mall.lib.repository.*;
import com.rockcent.service.ProductSrv;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangjh on 2017/1/23.
 */
@Service
public class ProductSrvImpl implements ProductSrv {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private MerchantProductRepository merchantProductRepository;
    @Autowired
    private EvaluationRepository evaluationRepository;
    @Autowired
    private MerchantOrderRepository merchantOrderRepository;
    @Autowired
    private MallProductRepository mallProductRepository;

    @Override
    public void addEv(Long customerDtoId, EvaluationDto evaluationDto, MerchantOrder merchantOrder) {
        String customerNickName = null;
        if (null != customerDtoId) {
            Customer customer = customerRepository.findOne(customerDtoId);
            customerNickName = customer.getNickName();
        }

        List<Evaluation> evaluationList = evaluationDto.getEvaluationList();
        for(Evaluation evaluation:evaluationList){
            Product product = merchantProductRepository.findOne(evaluation.getProductId());
            evaluation.setCustomerId(customerDtoId);
            if (!evaluation.getHideName()) {
                if(StringUtils.isBlank(evaluation.getCommentCustomerName())){
                    evaluation.setCommentCustomerName(customerNickName);
                }

            }
            evaluation.setDateCreated(new Date());
            evaluation.setLastUpdated(evaluation.getDateCreated());

            if(null==evaluation.getMerchantId()){
                evaluation.setMerchantId(product.getMerchantId());

            }
            if(StringUtils.isBlank(evaluation.getProductName())){
                evaluation.setProductName(product.getName());
            }
            if (evaluation.getProductId()==null){
                evaluation.setProductId(product.getId());
            }
            if (StringUtils.isBlank(evaluation.getMerchantOrderNum())){
                evaluation.setMerchantOrderNum(merchantOrder.getOrderNum());
            }
            if (StringUtils.isBlank(evaluation.getComment())) {
                evaluation.setComment("好评");
            }
        }
//        evaluationDto.getEvaluationList().stream().map(evaluation -> {
//
//            return evaluation;
//        }).collect(Collectors.toList());
        evaluationRepository.save(evaluationDto.getEvaluationList());
        //修改订单状态 EvaluateStatus -> HAD_EVALUATE（自营、分销）
        if (merchantOrder != null && merchantOrder.getEvaluateStatus() != EvaluateStatus.HAD_EVALUATE) {
            merchantOrder.setEvaluateStatus(EvaluateStatus.HAD_EVALUATE);
            merchantOrderRepository.save(merchantOrder);
        }
        if (merchantOrder != null && StringUtils.isNotBlank(merchantOrder.getMarketOrderNum())) {
            //如果是分销，需要改供应商的评价状态
            MerchantOrder merchantOrderMarket = merchantOrderRepository.findByMarketOrderNumAndMerchantRole(merchantOrder.getMarketOrderNum(), MerchantOrderMerchantRole.SUPPLIER);
            if (merchantOrderMarket != null && merchantOrderMarket.getEvaluateStatus() != EvaluateStatus.HAD_EVALUATE) {
                merchantOrderMarket.setEvaluateStatus(EvaluateStatus.HAD_EVALUATE);
                merchantOrderRepository.save(merchantOrderMarket);
            }
        }
    }

    /**
     * 添加精选产品的分页列表
     * @param mallId
     * @param type
     * @param productSourceType
     * @param merchantName
     * @param keyword
     * @param categoryIds
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<ProductDto> findProducts(Long mallId, String type, String productSourceType, String merchantName, String keyword, List<Long> categoryIds, PageResult pageResult) {
        return merchantProductRepository.findProducts(mallId,type,productSourceType,merchantName,keyword,categoryIds,pageResult,null,null,null);
    }

    /**
     * 保存精选产品
     * @param mallId
     * @param productIds
     */
    @Override
    public void saveExplosionProduct(Long mallId, List<Long> productIds) {
        if(mallId==null || productIds==null || productIds.size()==0) {
            return;
        }
        List<Product> products = merchantProductRepository.findByIdInAndMallId(productIds,mallId);
        if(products!=null && products.size()>0) {
            for(Product product:products) {
                product.setProductClientType(ProductClientType.EXPLOSION);
                product.setSortNum(1L);
            }
            merchantProductRepository.save(products);
        }
    }

    /**
     * 保存精选产品
     * @param mallId
     * @param productIds
     */
    @Override
    public void delExplosionProduct(Long mallId, List<Long> productIds) {
        if(mallId==null || productIds==null || productIds.size()==0) {
            return;
        }
        List<Product> productList = merchantProductRepository.findByIdInAndMallId(productIds,mallId);
        if(productList!=null && productList.size()>0) {
            for(Product product:productList) {
                product.setProductClientType(ProductClientType.GENERAL);
            }
            merchantProductRepository.save(productList);
        }
    }

    /**
     * 精选产品的分页列表
     * @param mallId
     * @param type
     * @param productClientType
     * @param merchantName
     * @param keyword
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<ProductDto> findExplosionProducts(Long mallId, String type, ProductClientType productClientType, String merchantName, String keyword, PageResult pageResult, String sortByName, String sort) {
        return merchantProductRepository.findProducts(mallId,type,null,merchantName,keyword,null,pageResult,productClientType,sortByName,sort);
    }

    /**
     * C端精选产品的首页列表
     * @param mallId
     * @param productClientType
     * @param pageResult
     * @return
     */
    @Override
    public PageResult<Product> findMiniExplosionProducts(Long mallId, ProductClientType productClientType, PageResult pageResult) {
        return mallProductRepository.findMiniExplosionProducts(mallId,productClientType,pageResult);
    }

    @Override
    public Product findOne(Long productId) {
        return merchantProductRepository.findOne(productId);
    }
}
